<?xml version="1.0" encoding="UTF-8" ?>
<html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
        xmlns:calli="http://callimachusproject.org/rdf/2009/framework#">
<head>
    <title resource="?this">{rdfs:label} Sign In</title>
    <link rel="help" href="../../callimachus-for-web-developers" target="_blank" title="Help" />
    <script type="text/javascript" src="../assets/jshash/md5.js"></script>
    <script type="text/javascript">
    // <![CDATA[
    $(document).bind("calliLoggedIn", function(event) {
        var return_to = /&return_to=([^&]*)/.exec(window.location.search);
        if (return_to && return_to[1]) {
            return_to = decodeURIComponent(return_to[1]);
        } else {
            return_to = '/';
        }
        window.location.replace(return_to);
    });
    $(function(){
        $('#form').submit(function(event){
            var username = $("#username").val();
            var authName = $("#authName").text();
            var password = $("#password").val();
            var persistent = $('#persistent:checked').val();
            var options = {type: "POST", url: "?login",
                username: username,
                password: password,
                xhrFields: calli.withCredentials,
                contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                data: {persistent: persistent},
                success: function(doc) {
                    if (persistent && window.localStorage) {
                        var hash = hex_md5(username + ':' + authName + ':' + password);
                        var digestPassword = hex_md5(hash + ':' + doc);
                        localStorage.setItem("username", username);
                        localStorage.setItem("digestPassword", digestPassword);
                    }
                    $(document).trigger("calliLoggedIn");
                }
            };
            try {
                jQuery.ajax(options);
            } catch (e) {
                // Opera don't support spaces in ajax passwords
                if (options.password) {
                    delete options.password;
                    jQuery.ajax(options);
                }
            }
            event.preventDefault();
            return false;
        });
    });
    // ]]>
    </script>
</head>
<body resource="?this">
    <hgroup>
        <h1>{rdfs:label} Sign In</h1>
        <h2 id="authName">{calli:authName}</h2>
    </hgroup>
    <p>{rdfs:comment}</p>
    <form role="form" id="form" method="POST" action="?login">
        <div class="form-group">
            <label for="username">Username or email</label>
            <div class="controls">
                <input type="text" class="form-control" id="username" placeholder="Username or email" required="required" autofocus="autofocus" />
            </div>
        </div>
        <div class="form-group">
            <label for="password">Password</label>
            <div class="controls">
                <input type="password" class="form-control" id="password" placeholder="Password" />
            </div>
        </div>
        <div class="form-group">
            <div class="controls">
                <label class="checkbox">
                    <input type="checkbox" id="persistent" value="true" /> Stay signed in
                </label>
            </div>
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary">Sign in</button>
            <a href="?daypass" class="btn btn-default">Need another password?</a>
        </div>
    </form>
</body>
</html>
